package com.goods.controller.business;
import com.goods.business.service.HealthServices;
import com.goods.common.error.BusinessCodeEnum;
import com.goods.common.model.system.User;
import com.goods.common.response.ResponseBean;
import com.goods.common.utils.JWTUtils;
import com.goods.common.vo.business.HealthVO;
import com.goods.common.vo.system.PageVO;
import com.goods.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ：MJS
 * @date ：Created in 2021/6/10  13:59
 * @description：TODO
 */
@RestController
@RequestMapping("/business/health")
public class HealthController {
    @Autowired
    private HealthServices healthServices;
    @Autowired
    private UserService userService;
    //今日是否已签到
    @GetMapping("/isReport")
    public ResponseBean isReport(HttpServletRequest request) {
        String authorization = request.getHeader("Authorization");
        String username = JWTUtils.getUsername(authorization);
        User user = userService.findUserByName(username);
        Long id = user.getId();
        if (user == null) {
            return ResponseBean.error(BusinessCodeEnum.USER_ACCOUNT_NOT_FOUND);
        }
       return healthServices.isReport(id);
    }
    //   post /report
    @PostMapping("/report")
    public ResponseBean report(@RequestBody HealthVO healthVO, HttpServletRequest request) {
        String authorization = request.getHeader("Authorization");
        String username = JWTUtils.getUsername(authorization);
        User user = userService.findUserByName(username);
        Long id = user.getId();
        if (user == null) {
            Map<String, String> map = new HashMap<>();
            map.put("errMsg", "打卡失败");
            return ResponseBean.error(map);
        }
        healthServices.report(healthVO, id);
        return ResponseBean.success(healthVO);
    }



    @GetMapping("/history")
    public ResponseBean<PageVO<HealthVO>> history(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                    @RequestParam(value = "pageSize",defaultValue = "6") Integer pageSize,
                                                    HealthVO healthVO){
        PageVO<HealthVO>healthVOList=healthServices.history(pageNum,pageSize,healthVO);
        return ResponseBean.success(healthVOList);
    }
}
